package com.lfy.commons.init;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Objects;
import java.util.concurrent.Executor;

/**
 *  初始化容器执行
 *
 * @author <a href="https://www.lvfeiy.top">吕飞扬</a>
 * @date 2023/10/13 14:06
 */

@Component
@Slf4j
public class ApplicationRun implements ApplicationListener<ContextRefreshedEvent> {
    @Resource
    private Executor primaryThreadPool;
    @Override
    public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {

        ApplicationContext applicationContext = contextRefreshedEvent.getApplicationContext();
        if(Objects.isNull(applicationContext.getParent())){
            applicationContext.getBeansOfType(ApplicationInit.class).forEach((k,v) ->{
                primaryThreadPool.execute(v::init);
                log.info("{} ...init",k);
            });
        }


    }
}
